<?php


namespace App\Department\Actions\Review;

use App\PatentApply;
use App\Setting;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use App\Department\Actions\RowAction;

class DeptReview extends RowAction
{
    public $name = '审批';

    public function handle($model, Request $request)
    {
        $status = $request->get('status') == 1 ? 1 : 2;
        $remark = $request->get('remark') ?: '';
        $review_name = Setting::getReviewName(2);
        try {
            $model->review_status = 0;
            $model->dept_review_status = $status;
            if ($model instanceof PatentApply) {
                $model->dept_review_remark = $remark;
            } else {
                $model->review_remark = $remark;
            }
            $model->save();
            if (method_exists($model, 'remarks')) {
                $model->remarks()->create([
                    'name'    => $review_name . '-' . data_get([1 => '通过', 2 => '未通过'], $status),
                    'user_id' => Admin::user()->id,
                    'remark'  => $remark
                ]);
            }
        } catch (\Exception $exception) {
            return $this->response()->swal()->error('操作失败！')->refresh();
        }
        return $this->response()->success('操作成功！')->location('');
    }

    public function form()
    {
        $this->radio('status', '请选择审核状态')
            ->options([1 => '通过', 2 => '不通过'])
            ->required();
        $this->textarea('remark', '科室意见');
    }

}
